package org.cuit.lmq.domain.common;

import lombok.extern.slf4j.Slf4j;
import org.cuit.lmq.constants.AppGlobal;
import org.cuit.lmq.domain.security.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Seon
 * @date 2023/3/4
 */
@Slf4j
public class BaseContextHolder {
    /**
     * 把SpringSecurity的代码封装了一下。
     *
     * @return 当亲用户id
     */
    public static long getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        if (AppGlobal.ANONYMOUS_USER.equals(principal)) {
            return 0L;
        } else {
            LoginUser loginUser = (LoginUser) principal;
            return loginUser.getUser().getId();
        }
    }
}
